<template>
	<view class="template-order">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar :bottomShadow="false" backgroundColor="#FFFFFF" customBack fixed>
			<view slot="back" class='tn-custom-nav-bar__back' @click="goBack">
				<text class='icon tn-icon-left-arrow'></text>
			</view>
			<view class="tn-flex tn-flex-col-center tn-flex-row-center ">
				<text class="tn-text-bold tn-text-xl tn-color-black">项目订单</text>
			</view>
		</tn-nav-bar>

		<view :style="{top: vuex_custom_bar_height + 20 +'px'}" class="order--wrap" :key="componentKey">
			<!-- 顶部标签 -->
			<view class="tn-bg-white">
				<tn-tabs-swiper ref="tabs" :current="tabsIndex" :isScroll="false" :list="list" activeColor="#3165CC"
					class="order__tabs tn-text-xl" inactiveColor="#080808" @change="tabsChange"></tn-tabs-swiper>
			</view>

			<!-- 标签内容 -->
			<swiper :current="swiperIndex" :style="{top: 60 +'px', height: `${swiperHeight}px`}"
				class="order__swiper" @animationfinish="swiperAnimationFinish" @transition="swiperTransition">
				<block v-for="(item,index) in resultList" :key="index">
					<swiper-item class="order__swiper__item">
						<scroll-view scroll-y v-if="item && item.length > 0" :style="{height: `${swiperHeight}px`}">
							<view v-for="e in item" :key="e.id" class="order__item"
								@click="handleItemClick(e)">
								<view
									class="order__item__head tn-flex tn-flex-direction-row tn-flex-col-center tn-flex-row-between">
									<view class="order__item__head__title" @click.stop="copyFun(e.orderId)">
										<text class="tn-text-bold">订单编号：{{ e.orderId }}</text>
										<text
											class="tn-color-blue--disabled tn-padding-left-xs tn-text-df tn-icon-copy"></text>
									</view>
									<tn-tag :backgroundColor="`tn-main-gradient-${getBgColor(e.orderPayStatus)}--light`"
										:fontColor="`tn-color-${getFontColor(e.orderPayStatus)}`" shape="circle"
										height="48rpx" width="100">
										{{getText(e.orderPayStatus)}}
									</tn-tag>
								</view>

								<view
									class="order__item__content tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-between">
									<view class="order__item__content__title">
										{{ e.title }}
									</view>
									<view
										class="order__item__content__info tn-flex tn-flex-direction-column tn-flex-col-center tn-flex-row-center">
										<view class="order__item__content__info__price">
											<text class="order__item__content__info__price--unit">￥</text>
											<text
												class="order__item__content__info__price__value--integer">{{ e.totalPrice }}</text>
										</view>
									</view>
								</view>

								<view
									class="order__item__operation tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-between">
									<view class="order__item__operaation__left">
										<text
											class="order__item__operaation__left--text tn-color-gray">{{e.createTime}}</text>
									</view>
									<view
										class="order__item__operation__right tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-right">
										<view class="order__item__operation__right__button">
											<tn-button v-if="e.orderPayStatus === 7" size="sm" shape="round" backgroundColor="#4080FF" fontColor="#FFFFFF" @click.stop="handleItemClick(e)">确认收货</tn-button>
										</view>
									</view>
								</view>
							</view>
							<view class="tn-padding-bottom"></view>
						</scroll-view>
						<view v-else>
							<tn-empty mode="order"></tn-empty>
						</view>
					</swiper-item>
				</block>
			</swiper>
		</view>


	</view>
</template>

<script>
	import templatePageMixin from '@/libs/mixin/template_page_mixin.js'
	import {
		getOrderStatusList,
		getProjectOrderStatusList
	} from "@/api/user";

	export default {
		name: 'TemplateOrder',
		mixins: [templatePageMixin],
		data() {
			return {
				list: [{
						name: '全部'
					},
					{
						name: '待付款'
					},
					{
						name: '已付款'
					},
					{
						name: '已完成'
					}
				],
				componentKey: 0,
				tabsIndex: 0,
				swiperIndex: 0,
				swiperTop: 20,
				swiperHeight: 0,
				resultList: [[], [], [], []],
				orderStatus: global.staticDict.orderStatus,
			}
		},
		onLoad(param) {
			this.type = param.type
		},
		async onShow() {
			await this.queryOrderStatusList(this.type)
			this.swiperIndex = this.tabsIndex
			this.componentKey++;
		},
		onReady() {
			this.$nextTick(() => {
				this.updateSwiperInfo()
			})
			this.tabsIndex = this.type
		},
		computed: {},
		methods: {
			copyFun(orderCode) {
				uni.setClipboardData({
					data: orderCode,
				})
			},
			handleItemClick(item) {
				if (item.orderPayStatus === 7) {
					this.tn(`/minepages/orderEndDetail?id=${item.id}`)
				} else {
					this.tn(`/homepages/detail?id=${item.id}&orderId=${item.orderId}`)
				}
			},
			//标签背景颜色
			getBgColor(status = 1) {
				return this.orderStatus[status].bgColor
			},
			//标签字体颜色
			getFontColor(status = 1) {
				return this.orderStatus[status].color
			},
			//标签文本
			getText(status = 1) {
				return this.orderStatus[status].text
			},
			async queryOrderStatusList(type) {
				try {
					const temp = ['status_all', 'status_wait_pay',
						'paid',
						'status_end'
					]
					for (const item of temp) {
						const res = await getProjectOrderStatusList(item)
						if (res.code === 200) {
							this.resultList[temp.indexOf(item)] = res.data
						} else {
							uni.showToast({
							  title: "系统异常",
							  duration: 1500,
							  icon: 'error'
							});
							this.resultList[temp.indexOf(item)] = []
						}

					}
				} catch (e) {
					console.log(e)
				}
			},
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},

			// 计算可滑动区域的高度信息
			updateSwiperInfo() {
				// 获取可滑动菜单的信息
				this._tGetRect('.order__tabs').then(res => {
					if (!res) {
						setTimeout(() => {
							this.updateSwiperInfo()
						}, 10)
						return
					}
					const systemInfo = uni.getSystemInfoSync()
					this.swiperTop = res.bottom - this.vuex_custom_bar_height
					this.swiperHeight = systemInfo.safeArea.height - res.bottom + systemInfo.statusBarHeight
				})
			},
			// 标签栏值发生改变
			tabsChange(index) {
				this.swiperIndex = index
			},
			// swiper-item位置发生改变
			swiperTransition(event) {
				this.$refs.tabs.setDx(event.detail.dx)
			},
			// swiper动画结束
			swiperAnimationFinish(event) {
				const current = event.detail.current
				this.$refs.tabs.setFinishCurrent(current)
				this.swiperIndex = current
				this.tabsIndex = current
			}
		}
	}
</script>

<style lang="scss" scoped>
	.template-order {
		background-color: #FFFFFF;
	}

	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 60%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

	}

	.order {
		&--wrap {
			position: fixed;
			left: 0;
			right: 0;
			width: 100%;
			background-color: inherit;
		}

		/* 导航栏 start */
		&__tabs {
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			background-color: inherit;
		}

		/* 导航栏 end */

		/* swiper start */
		&__swiper {
			position: absolute;
			left: 0;
			right: 0;
			width: 100%;
			background-color: inherit;
			// padding: 0 16rpx;
		}

		/* swiper end */

		/* 订单内容 start */
		&__item {
			margin: 30rpx;
			padding: 36rpx 26rpx;
			background-color: #FFFFFF;
			border-radius: 15rpx;
			box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.07);

			&:first-child {
				margin-top: 40rpx;
			}

			&:last-child {
				margin-bottom: 0;
			}

			/* 头部 start */
			&__head {

				&__title {
					padding-bottom: 10rpx;
					line-height: normal;

					&--right-icon {
						font-size: 24rpx;
						margin-left: 8rpx;
					}
				}

				&__status {
					font-size: 22rpx;
					color: #AAAAAA;
				}
			}

			/* 头部 end */

			/* 内容 start */
			&__content {

				margin-top: 20rpx;

				&__image {
					margin-right: 20rpx;

					image {
						width: 140rpx;
						height: 140rpx;
						border-radius: 10rpx;
					}
				}

				&__title {
					padding-right: 40rpx;
					display: -webkit-box;
					overflow: hidden;
					white-space: normal !important;
					text-overflow: ellipsis;
					word-wrap: break-word;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}

				&__info {

					&__price {
						&--unit {
							font-size: 20rpx;
						}

						&__value--integer,
						&__value--decimal {
							font-weight: bold;
						}

						&__value--decimal {
							font-size: 20rpx;
						}
					}

					&__count {
						color: #AAAAAA;
						font-size: 24rpx;
					}
				}
			}

			/* 内容 end */

			/* 操作按钮 start */
			&__operation {
				margin-top: 30rpx;

				&__right {
					&__button {
						margin-left: 10rpx;
					}
				}
			}

			/* 操作按钮 end */
		}

		/* 订单内容 end */
	}
</style>